<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/28
 * Time: 11:41
 */
namespace app\index\controller;

use think\Db;

class Postpayment extends Common {
	//判断是否登录
	public function _initialize()
	{
		parent::_initialize();
	}
	
	/**
	 * 后付款的订单
	 * @param string get.ordernumber 订单号
	*/
	public function index()
	{
		$where = ['query'=>[]];
		$db = Db::table('postpayment')->field('postpayment.ordernumber,postpayment.date,shop.address')
			->join('shop','shop.id = postpayment.sid')
			->where('postpayment.status','<>',0)
			->group('postpayment.ordernumber');
		if(!empty(trim(input('get.ordernumber')))){
			$ordernumber = trim(input('get.ordernumber'));
			$db->where('postpayment.ordernumber',$ordernumber);
			$where['query']['ordernumber'] = $ordernumber;
		}
		if(!empty(trim(input('get.status')))){
			$db->whereNotIn('postpayment.status',[0,5]);
			$where['query']['status'] = 2;
		}
		$result = $db->order('postpayment.id desc')->paginate(8,false,$where);
		return view('index',compact('result','ordernumber'));
	}
	
	/**
	 * 订单详情
	 * @param string get.ordernumber 订单号
	*/
	public function detail()
	{
		$ordernumber = input('get.ordernumber');
		$result = Db::table('postpayment')
			->field('product.pname,product.purchaseprice,product.retailprice,business.linkman,
					postpayment.total,postpayment.actualtotal,postpayment.number,postpayment.status,
					postpayment.ordernumber,postpayment.id,postpayment.date,postpayment.bid,
					postpayment.express,postpayment.oddnumber')
			->join('product','product.id = postpayment.pid','LEFT')
			->join('shop','shop.id = postpayment.sid','LEFT')
			->join('business','business.id = postpayment.bid','LEFT')
//			->join('inventory','inventory.postid = postpayment.id','RIGHT')
			->where('postpayment.ordernumber',$ordernumber)
			->where('postpayment.status','<>',0)
			->order('postpayment.id desc')
			->paginate(10);
		return view('detail',compact('result'));
	}
	
	/**
	 * 订单详情
	 * @param string get.id 订单id
	*/
	public function orderDetail()
	{
		$id = input('get.id');
		$result = Db::table('postpayment')
			->join('shop','shop.id = postpayment.sid','LEFT')
			->join('business','business.id = postpayment.bid','LEFT')
			->join('product','product.id = postpayment.pid','LEFT')
			->where('postpayment.id',$id)
			->find();
		$result['image'] = json_decode($result['image']);
		$status = Db::table('postpayment')->where('id',$id)->value('status');
		return view('orderdetail',compact('result','id','status'));
	}
	
	/**
	 * 修改购买个数
	 * @param string post.id 订单id
	 * @param string post.number 购买数量
	 * @param string post.purchaseprice 进价
	*/
	public function updateTotal()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$number = input('post.number');
				$purchaseprice = input('post.purchaseprice');
				$input['total'] = $number * $purchaseprice;
				$input['number'] = $number;
				$result = Db::table('postpayment')->where('id',$id)->update($input);
				if(!$result){
					throw new \PDOException('修改失败');
				}
				$success = ['success'=>'修改成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 添加实际总价
	 * @param string post.id 订单id
	 * @param string post.actualtotal 实际总价
	*/
	public function total()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$input['actualtotal'] = input('post.actualtotal');
				$result = Db::table('postpayment')->where('id',$id)->update($input);
				if(!$result){
					throw new \PDOException('添加失败');
				}
				$success = ['success'=>'添加成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 *修改订单状态
	 * @param string post.status 订单状态
	 * @param string post.id 订单id
	*/
	public function updatestatus()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$status = input('post.status');
				//t 代表通过
				if($status == 't'){
					$result = Db::table('postpayment')->where('id',$id)->update(['status'=>2]);
					if(!$result){
						throw new \PDOException('操作失败');
					}
				}
				//j代表邮寄
				if($status == 'j'){
					$result = Db::table('postpayment')->where('id',$id)->update(['status'=>3]);
					if(!$result){
						throw new \PDOException('操作失败');
					}
				}
				$success = ['success'=>'操作成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 货已到
	 * @param string get.id 订单号
	*/
	public function reach()
	{
		try{
			$id = input('get.id');
			Db::startTrans();
			$result = Db::table('postpayment')->where('id',$id)->update(['status'=>5]);
			if($result){
				$value = Db::table('postpayment')->where('id',$id)->find();
				if($value){
					$val = Db::table('inventory')->where('sid',$value['sid'])->where('pid',$value['pid'])->find();
					if($val){
						$val['number'] += $value['number'];
						$res = Db::table('inventory')->where('sid',$value['sid'])->where('pid',$value['pid'])->update(['number'=>$val['number']]);
						if(!$res){
							throw new \PDOException('库存更新失败');
						}
					}else{
						$res = Db::table('inventory')->insertGetId(['bid'=>$value['bid'],'sid'=>$value['sid'],'pid'=>$value['pid'],'postid'=>$value['id'],'number'=>$value['number']]);
						if(!$res){
							throw new \PDOException('库存添加失败');
						}
					}
				}else{
					throw new \PDOException('订单已失效');
				}
			}else{
				throw new \PDOException('订单凭证添加失败');
			}
			Db::commit();
			$success = ['success'=>'操作成功'];
			return json($success);
		}catch (\PDOException $e){
			Db::rollback();
			$error  = ['error'=>$e->getMessage()];
			return json($error);
		}
	}
	
	/**
	 *查看存货数量
	 * @param string get.id 订单号
	*/
	public function stock()
	{
		$id = input('get.id');
		$result = Db::table('inventory')
				->field('product.pname,inventory.number')
				->join('product','product.id = inventory.pid','LEFT')
				->join('postpayment','postpayment.id = inventory.postid','LEFT')
				->where('inventory.postid',$id)
				->find();
		return view('stock',compact('result'));
	}
	/**
	 * 订单删除
	 * @param string post.id 订单id
	*/
	public function delete()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$result = Db::table('postpayment')->where('id',$id)->update(['status'=>0]);
				if(!$result){
					throw new \PDOException('删除失败');
				}
				$success = ['success'=>'删除成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 添加物流信息页面
	 * @param string get.id 采购单id
	*/
	public function express()
	{
		$id = input('get.id');
		return view('express',compact('id'));
	}
	/**
	 * 添加物流信息
	 * @param string post.id 采购单id
	 * @param string post.oddnumber 运单号
	 * @param string post.express 快递公司
	 */
	public function logistics()
	{
		if(request()->isPost()){
			try{
				$id = input('post.id');
				$input['oddnumber'] = input('post.oddnumber');
				$input['express'] = input('post.express');
				$result = Db::table('postpayment')->where('id',$id)->update($input);
				if(!$result){
					throw new \PDOException('添加失败');
				}
				$success = ['success'=>'添加成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
}